import socket


def main():
    # 创建套接字
    tcp_server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

    # 绑定信息,绑定的要一元组显示
    tcp_server.bind(('192.168.124.32',7788))

    # 设为被动连接
    tcp_server.listen(128)
    while True:
        # 等待接入
        client_socket,client_addr = tcp_server.accept()

        # 获取下载文件的名称
        recv = client_socket.recv(1024)
        recv = recv.decode('utf-8')

        # 打开要下载的文件
        send_date = None
        try:
            f = open(recv,'r')
            send_date = f.read()
            f.close()
        except Exception as ret:
            print('没有要下载的文件（%s）'% recv)

        # 发送给客户端
        if send_date:
            client_socket.send(send_date.encode('utf-8'))

        #关闭套接字
        client_socket.close()
    tcp_server.close()

if __name__=='__main__':
    main()
